#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

#include "./comm_head.h"
/*
   测试数据：
   输入：
   0x20000000,0x003,0x01,0x02,0x03,0x55,0x00,0xaa,79,9a
   输出：
   0x0 0x0 0x0 0x20 0x3 0x0 0x1 0x2 0x3 0x55 0x0 0xaa 0x79 0x9a
*/
char arg[4096];
u_int8_t ByteData[4096];
u_int8_t ByteInData[4096];
bool ascii2hex(char** q, int* ret_val, long* pcount);
int  valuecnt(long val);
int main(int argc, char* argv[])
{
	/*
	 * 打印输入的字符串参数并拼接成一个完整字符串
	 */
	int i,offset = 0;	
	printf("argc is: %d \n", argc);
	for(i = 1; i < argc; i++)
	{
		printf("argv[%d] is: %s\n",i, argv[i]);
		strcpy(&arg[offset], argv[i]);
		offset += strlen(argv[i]);
	}
	printf("arg is: %s \n",arg);

	/*
	 * 将字符串转换为对应的16进制数据
	 */
	char* p = &arg[0];
	int  out_len = 0, tmp = 0;
        long cnt = 0;
	while(*p != 0)
	{
		if(ascii2hex(&p,&tmp,&cnt))
		{
#if 1
			cnt = valuecnt(cnt);
			do	
			{	
				ByteData[out_len++] = tmp;
				tmp >>= 8;
			}while(--cnt);
#endif
		}
	}

	printf("ByteData: \n");
	for(i = 0; i < out_len; i++)
	{
		printf("0x%x ", ByteData[i]);	
	}
	
	printf("\n");


	/*
	 * 组成数据帧格式发送到串口
	 */
	int r_len;	
	open_and_wr_serial(ByteData,out_len,ByteInData, 4096, &r_len);
	
	printf("ByteInData: \n");
	for(i = 0; i < r_len; i++)
	{
		printf("0x%x ", ByteInData[i]);	
	}




	return 0;
}

int  valuecnt(long val)
{
	if(val > 0xFFFFFF)
		return 4;
	else if(val > 0xFFFF)
		return 3;
	else if(val > 0xFF)
		return 2;
	else 
		return 1;
}
/**
  * @brief 
  */
bool my_is_big_char(char ch)
{
	if(ch >= 'A' && ch <= 'F')
		return true;
	else
		return false;
}

bool my_is_small_char(char ch)
{
	if(ch >= 'a' && ch <= 'f')
		return true;
	else
		return false;
}
bool my_isdigit(char ch)
{
	if(ch >= '0' && ch <= '9') 
		return true;
	else
		return false;
}

int dec2hex(int dec)
{
	int hex = 0;
	int val = 0;
	int loop = 0;
	do{
		val = (dec % 10) << (4*loop);
		hex |=  val;
		dec /= 10;
		loop++;
	}while(dec);
	return hex;
}
#if 0
uint8_t ascii2hex(char** q)
{
	char*p = *q;

	do{	
		if(isdigit(*p))
		{
			if(((*p == '0') && (*(p+1) == 'x')) || ((*p == '0') && (*(p+1) == 'X'))) /* '0x' '0X' */
			{
				p += 2;
				continue;	
			}	
			hex <<= 4;
			hex |=  (*p - '0');	               /* 0 1 2 3 4 5 6 7 8 9 */	
		}
		else if(my_is_big_char(*p))            /* A B C D E F */
		{
			hex <<= 4;
			hex |=  (*p - 'A') + 10;		
		}
		else if(my_is_small_char(*p))          /* a b c d e f */
		{
			hex <<= 4;
			hex |=  (*p - 'a') + 10;		
		}
		else
		{
			break;	
		}
		p++;
	}while(*p != 0); 
	

}
#endif

/**
  * @brief
  * @example  1a -> 0x1a
  *           0X9B, -> 0x9b
  *           0xab 0xcd   --> 0xcdab
  */
bool ascii2hex(char** q, int* ret_val, long* pcount)
{
	int hex = 0;
	int loop = 0;
	char* p = *q;
	long cnt = 0;
FILTER_STR:
	while((isdigit(*p) == false) && (my_is_big_char(*p) == false) && (my_is_small_char(*p) == false) && (*p != 0))
	{
		p++;
	}
	if(*p == 0)
	{
		*q = p; 
		return false;
	}

	do{	
		if(isdigit(*p))
		{

			if(((*p == '0') && (*(p+1) == 'x')) || ((*p == '0') && (*(p+1) == 'X'))) /* '0x' '0X' */
			{
				p += 2;
				goto FILTER_STR;	
			}	
			hex <<= 4;
			hex |=  (*p - '0');	               /* 0 1 2 3 4 5 6 7 8 9 */	
			cnt <<= 4;
			cnt |= 0x0F;
		}
		else if(my_is_big_char(*p))            /* A B C D E F */
		{
			hex <<= 4;
			hex |=  (*p - 'A') + 10;		
			cnt <<= 4;
			cnt |= 0x0F;
		}
		else if(my_is_small_char(*p))          /* a b c d e f */
		{
			hex <<= 4;
			hex |=  (*p - 'a') + 10;		
			cnt <<= 4;
			cnt |= 0x0F;
		}
		else
		{
			break;	
		}
		p++;
	}while(*p != 0); 
	*q = p; 
	*ret_val = hex;
	*pcount = cnt;

	return true; 
}

